package com.xx.hdfsFx.promptStage;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import javax.rmi.CORBA.Util;
import java.util.Objects;

import static com.xx.util.Util.getResourceUrl;

public class PromptPaneTool {
    public final Stage nowStage;
    private double offsetX;
    private double offsetY;
    private final VBox infoVb = new VBox(30);
    private final BorderPane root = new BorderPane(infoVb);

    public PromptPaneTool() {
        nowStage = new Stage();
        nowStage.initModality(Modality.APPLICATION_MODAL);
        infoVb.setPadding(new Insets(40, 0, 0, 10));
        infoVb.setAlignment(Pos.CENTER_LEFT);
        root.setStyle("-fx-background-color:" +
                "linear-gradient(from 0.0% 65.9% to 100.0% 100.0%, #308ef2cc 0.0%, #00eeffff 100.0%);");
    }

    public void addTitle(Image icon, String title, Image close) {
        BorderPane bp = new BorderPane();

        ImageView iconView = new ImageView(icon);
        iconView.setPreserveRatio(true);
        iconView.setFitWidth(50);
        Label iconL = new Label();
        iconL.setPrefSize(60, 60);
        iconL.setAlignment(Pos.CENTER);
        iconL.setGraphic(iconView);

        Text titleText = new Text(title);
        Label textL = new Label();
        textL.setGraphic(titleText);
        textL.setGraphicTextGap(0);

        ImageView closeView = new ImageView(close);
        closeView.setPreserveRatio(true);
        closeView.setFitWidth(50);
        Label closeL = new Label();
        closeL.setPrefSize(60, 60);
        closeL.setAlignment(Pos.CENTER);
        closeL.setGraphic(closeView);
        closeL.setGraphicTextGap(0);

        closeL.setOnMouseEntered(event -> closeL.setStyle("-fx-background-color: red"));
        closeL.setOnMouseExited(event -> closeL.setStyle("-fx-background-color: null"));
        closeL.setOnMouseClicked(event -> nowStage.close());

        bp.setLeft(iconL);
        bp.setCenter(textL);
        bp.setRight(closeL);

        root.setTop(bp);
    }

    public void addTitle(Image icon, String title) {
        addTitle(icon, title, new Image(getResourceUrl("/texture/close.png")));
    }

    public void addTitle(String title) {
        addTitle(new Image(getResourceUrl("/texture/icon2.png")), title);
    }

    public void addInfo(String itemName, String info) {
        HBox hb = new HBox(10);
        hb.setAlignment(Pos.CENTER_LEFT);
        hb.setPadding(new Insets(10));
        Text itemText = new Text(itemName);
        itemText.setFont(Font.font("华文中宋", 25));
        Text infoText = new Text(info);
        infoText.setFont(Font.font("华文中宋", 25));
        hb.getChildren().addAll(itemText, infoText);
        TextField t = new TextField();
        infoVb.getChildren().add(hb);
    }

    public TextField addInput(String itemName, String info, String promptText, Label... labels) {
        TextField textField = addInput(itemName, info, labels);
        textField.setPromptText(promptText);
        textField.setFocusTraversable(false);
        return textField;
    }

    public TextField addInput(String itemName, String info, Label... labels) {
        HBox hBox = new HBox(10);
        hBox.setPadding(new Insets(10));
        hBox.setAlignment(Pos.CENTER_LEFT);
        HBox hb = new HBox(10);
        hb.setAlignment(Pos.CENTER_LEFT);
        hb.getStyleClass().add("hb-input");
        Text t = new Text(itemName);
        t.setFont(Font.font("华文中宋", 25));
        TextField textField = new TextField(info);
        textField.getStyleClass().add("textFiled-input");
        hb.getChildren().add(textField);
        hb.getChildren().addAll(labels);
        hBox.getChildren().addAll(t, hb);
        infoVb.getChildren().add(hBox);
        return textField;
    }

    public Button addButton(String ok, String cancel) {
        HBox hb = new HBox(30);
        hb.setPadding(new Insets(20));
        hb.setAlignment(Pos.CENTER_RIGHT);
        Button okBt = new Button(ok);
        okBt.getStyleClass().add("okBt-prompt");
        Button cancelBt = new Button(cancel);
        cancelBt.getStyleClass().add("cancelBt-prompt");
        cancelBt.setOnAction(event -> nowStage.close());
        hb.getChildren().addAll(okBt, cancelBt);
        root.setBottom(hb);
        return okBt;
    }

    public Text addErrorInfo(String info) {
        Text text = new Text(info);
        text.setVisible(false);
        text.setFont(Font.font("幼圆", 25));
        text.setFill(Color.RED);
        infoVb.getChildren().add(text);
        return text;
    }

    public void initStage(double preferWidth, double preferHigh) {
        root.setPrefSize(preferWidth, preferHigh);
        Scene scene = new Scene(root);
        scene.getStylesheets().add(Objects.requireNonNull(
                Util.class.getResource("/css/hdfsApp.css")).toExternalForm());
        scene.setOnMousePressed(event -> {
            offsetX = event.getSceneX();
            offsetY = event.getSceneY();
        });
        scene.setOnMouseDragged(event -> {
            nowStage.setX(event.getScreenX() - offsetX);
            nowStage.setY(event.getScreenY() - offsetY);
        });
        nowStage.setScene(scene);
        nowStage.initStyle(StageStyle.TRANSPARENT);
    }
}
